﻿var url 											= "http://integration:8888/status";
var integrationIp					= "integration"
var result										= null;
var belet										= "belet";
var vrij												= "vrij";
var originalRefresh		  = 3*1000; 			// 5 sec
var errorRefresh          = 2*60*1000;	// 2 min
var counter								= 0;
var reset 										= false;
var timer										= null;
var response 							= "";
var requestIntegration = false;


function connect_it()
{
	var launcher = new ActiveXObject("WScript.Shell");
  launcher.Run("mstsc.exe /f /console /v:" + integrationIp);
}


function view_onPopout()
{
	showDetails();
}


function showDetails()
{
  var htmlDetailsView = new DetailsView();
  htmlDetailsView.html_content = true;
  htmlDetailsView.setContent('', undefined, response, false, 0);

  var externalObject = new Object();
  externalObject.onItemClick = onItemClick;
  htmlDetailsView.external = externalObject;

  pluginHelper.showDetailsView(htmlDetailsView, getStatus(),  gddDetailsViewFlagToolbarOpen, onDetailsViewFeedback);
}


function onItemClick(text) 
{
  pluginHelper.CloseDetailsView();
}


function view_onOpen()
{
	debug.info("starting...");
  timer = setInterval(refreshStatus, originalRefresh); 
}


function refreshStatus()
{
  var http = new XMLHttpRequest();
	try {  
    http.onreadystatechange =  function() {
				if (http.readyState == 4) {
					response = http.responseText;
					result = (response.substring(response.lastIndexOf('<title>')+7, response.lastIndexOf('</title>')));
					status.innerText = getStatus();
					view.caption = getStatus();
					doRequestIntegration();
					counter = 0;
					if(reset) { setOriginalInterval() };
				} else { 
					response = "";
					counter++;
					if (counter > 6) {setNewInterval()}
					return; 
				} 
		}
		debug.info("Open url:  " + url); 
    http.open("GET", url, false); 
		http.setRequestHeader("Cache-Control", "no-cache"); 
    http.setRequestHeader("Pragma", "no-cache"); 
		http.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); 
    http.send(null); 
  } 
  catch(err) { 
    debug.error("Lap, een errorke: " + err); 
	  setNewInterval();
    return; 
  } 
	http = null;
}


function doRequestIntegration()
{
	if(requestIntegration != null && isVrij() && requestIntegration)
	{
		chkRequestIntegration.value = false;
		requestIntegration = false;
		connect_it();
	}
	setBgColor();
}


function setBgColor()
{
	isVrij() ? bgDiv.background = "#006600" : bgDiv.background = "#FF0000";
	setRemoteDesktopButton();
}


function setRemoteDesktopButton()
{
	if(isVrij()){ btnRemote.enabled = true; btnRemote.opacity=255} else {btnRemote.enabled = false; btnRemote.opacity=100};
}


function setOriginalInterval()
{
	clearInterval(timer);
  timer = setInterval(refreshStatus, originalRefresh); 
	reset = false;
}


function setNewInterval()
{
	clearInterval(timer);
	status.innerText = ".:: Offline ::.";
	bgDiv.background = "#FFA500";
	reset = true;
  timer = setInterval(refreshStatus, errorRefresh); 
}


function isBelet()
{
	return result == belet;
}


function isVrij()
{
	return result == vrij;
}


function getStatus()
{
	return "Status: " + result;
}

function onDetailsViewFeedback(detailsViewFlags) 
{
  if (detailsViewFlags == gddDetailsViewFlagNone) {
    // User closed the details view
  } else if (detailsViewFlags == gddDetailsViewFlagToolbarOpen) {
    // User clicked on the title of the details view
  }
}

function chkRequestIntegration_onchange() 
{
	requestIntegration = chkRequestIntegration.value;
}